<?php
/**
 * Created by 荣耀电竞.
 * User: 林子彦 <278805354@qq.com>
 * Date: 2018/6/12 0012
 * Time: 10:24
 */

namespace App\Shop\Controllers;

use App\Base\Controllers\Controller;
use App\Shop\Contracts\GoodsClassInterface;
use App\Shop\Contracts\GoodsInterface;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class AdminGoodsController extends Controller
{
    /**
     * 获取产品接口操作类
     * @return GoodsInterface
     */
    public function getGoodsInterface()
    {
        return app()->make(GoodsInterface::class);
    }

    /**
     * 产品分类操作类
     * @return GoodsClassInterface
     */
    public function getGoodsClassInterface()
    {
        return app()->make(GoodsClassInterface::class);
    }

    /**
     * 产品列表
     * @return $this
     */
    public function index(Request $request)
    {
        if ($request->getMethod() == "POST") {
            return $this->getGoodsInterface()->getGoodsCommonList([]);
        }
        $specList = $this->getGoodsInterface()->getSpecWithValueList([]);
        return view("goods.index")->with("spec_list", $specList);
    }

    /**
     * 添加
     * @param Request $request
     * @return $this
     */
    public function add(Request $request)
    {
        if ($request->getMethod() == "POST") {
        return     $this->saveGoodsData($request);
        }

        //获取产品分类
        $goodsClassList = $this->getGoodsClassInterface()->getGoodsClassList(["pid" => 0]);
        //获取规格表
        $specList = $this->getGoodsInterface()->getSpecWithValueList([]);
        return view("goods.add")->with("spec_list", $specList)->with("goods_class_list", $goodsClassList);
    }

    /**
     * 获取分类下的分类
     * @param Request $request
     * @return mixed
     */
    public function getSubGoodsClass(Request $request)
    {
        $id = $request->input("id");
        return $this->getGoodsClassInterface()->getGoodsClassList(["pid" => $id]);
    }

    /**
     * 保存产品数据
     * @param Request $request
     */
    public function saveGoodsData(Request $request){

        $validator=Validator::make($request->all(), [
            'gc_id' => 'required',
        ],["gc_id.required"=>'分类id不能为空']);
        if ($validator->fails()) {
            $this->throwValidatorException($validator);
        }
        return $this->getGoodsInterface()->saveGoodsData($request->all());
    }

    public function edit(Request $request){
        if ($request->getMethod() == "POST") {
            return     $this->updateGoodsData($request);
        }
        //产品id
        $goodsId=$request->route('goods_id');
        //获取产品分类
        $goodsClassList = $this->getGoodsClassInterface()->getGoodsClassList(["pid" => 0]);
        //获取规格表
        $specList = $this->getGoodsInterface()->getSpecWithValueList([]);
        //获取产品信息
        $goodInfo=$this->getGoodsInterface()->getGoodsCommonById($goodsId);

         //返回
        return view("goods.edit")->with("spec_list", $specList)->with("goods_class_list", $goodsClassList)->with("goods",$goodInfo);
    }

    /**
     * 产品编辑
     * @param Request $request
     * @throws \App\Base\Exceptions\ApiException
     */
    public function updateGoodsData(Request $request){
        $validator=Validator::make($request->all(), [
            'gc_id' => 'required',
        ],["gc_id.required"=>'分类id不能为空']);
        if ($validator->fails()) {
            $this->throwValidatorException($validator);
        }
      return   $this->getGoodsInterface()->updateGoodsData($request->all());
    }

    public function imgTlpDemo(){
        return view("goods.demo");
    }


}